
from odoo import api, models, fields


class LibraryBook(models.Model):
    _name = 'library.book'
    _description = '图书信息'
    _order = 'date_release desc, name'
    _rec_name = 'short_name'

    _sql_constraints = [
        ('name_uniq', 'UNIQUE (name)', '书名必须唯一。'),
        ('positive_page', 'CHECK(pages>0)', '页数必须是正数。')
    ]

    name = fields.Char('书名', required=True)
    date_release = fields.Date('出版日期')
    age_days = fields.Integer('出版天数',
                              compute='_compute_age',
                              store=False,
                              compute_sudo=True
                              )
    author_ids = fields.Many2many('res.partner', string='作者')

    short_name = fields.Char('书名简称', required=True,
                             translate=True, index=True)

    notes = fields.Text('备注')
    state = fields.Selection(string='状态',
                             selection=[('draft', '不可用'), ('available', '可用'), ('lost', '遗失')],
                             default="draft")
    description = fields.Html('内容简介')
    cover = fields.Binary('封面')
    out_of_print = fields.Boolean('是否绝版')
    date_updated = fields.Datetime('更新时间')
    pages = fields.Integer('页数',
                           groups='base.group_user',
                           states={'lost': [('readonly', True)]},
                           help='图书总页数')
    reader_rating = fields.Float('读者评价', digits=(14, 4))

    cost_price = fields.Float('采购价格', digits='Book Price')

    currency_id = fields.Many2one(string='货币类型', comodel_name='res.currency')
    retail_price = fields.Monetary('零售价格', currency_field='currency_id')

    publisher_id = fields.Many2one(string='出版社', comodel_name='res.partner',
                                   # optional
                                   ondelete='set null',  # 'restrict', 'cascade'
                                   context={},
                                   domain=[])

    category_id = fields.Many2one('library.book.category', '类别')

    @api.constrains('date_release')
    def _check_release_date(self):
        for record in self:
            if record.date_release and record.date_release > fields.Date.today():
                raise models.ValidationError('出版日期不能大于当前日期。')

    @api.depends('date_release')
    def _compute_age(self):
        today = fields.Date.today()
        for book in self:
            if book.date_release:
                delta = today - book.date_release
                book.age_days = int(delta.days)
            else:
                book.age_days = 0

